<template>
  <div>
    <PageTitle :title="titleName"></PageTitle>
    <div class="form-wrapper">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号">
          <el-select v-model="number" placeholder="请选择">
            <el-option
              v-for="item in numberList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="公司名称" prop="company">
          <el-input v-model="ruleForm.company" placeholder="请输入公司名称"></el-input>
        </el-form-item>
        <!-- <el-form-item label="分类状态" prop="state">
          <el-select v-model="ruleForm.state" placeholder="请选择类别状态">
            <el-option label="上架" value="2"></el-option>
            <el-option label="下架" value="1"></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="$router.go(-1)">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
	import PageTitle from "@/components/pageTitle/PageTitle.vue";
	import _ from "lodash";
	export default {
		name: "FirstTypeDetail",
		components: {
			PageTitle,
		},
  		activated() {
			if (this.$route.params.id != "none") {
				this.ruleForm.company = this.$route.params.company;
				this.number = this.$route.params.login_id
			}
			this.getList()
  		},
		deactivated() {
			this.$destroy();
		},
  		data() {
			return {
				number: null, //当前选中账号
				numberList: [], //账号列表
				ruleForm: {
					company: "",
				},
				rules: {
					company: [
						{ required: true, message: "请输入公司名称", trigger: "blur" },
					],
				},
			};
  		},
  		computed: {
			titleName() {
				return this.$route.params.id == "none" ? "添加公司" : "查看公司";
			},
			submitUrl() {
				return this.$route.params.id == "none"
					? this.$api + "/v1/company/company"
					: this.$api + "/v1/company/modify";
			},
  		},
  		methods: {
			//获取列表
			async getList(){
				let data = await this.$axios({
					url: this.$api + '/v1/order/brand',
					method: 'GET',
					params:{
						size: 10000,
						page: 1
					}
				})
				console.log('列表---------',data)
				if (data.data.code == 0) {
					this.numberList = data.data.data.item
				}
			},
    		submitForm: _.throttle(function () {
        		this.$refs.ruleForm.validate(async (valid) => {
					if (!valid) {return false;}
          			let { data: { code, data }} = await this.$axios({
            			url: this.submitUrl,
            			method: "POST",
						data: {
							id: this.$route.params.id,
							login_id:this.number,
							company: this.ruleForm.company,
						},
          			});
          			if (code == 0) {
            			this.$message.success(data);
						setTimeout(() => {
							this.$router.go(-1);
						}, 2000);
          			}
        		});
      		},2000,{ trailing: false }),
    		async _get() {
      			let {data: { code, data }} = await this.$axios({
				url: this.$api + "/v1/fication/surface",
				method: "GET",
				params: {
					id: this.$route.params.id,
				},
			});
			if (code == 0) {
				this.ruleForm.class_name = data[0].class_name;
				this.ruleForm.state = data[0].state;
			}
    	},
  	},
};
</script>

<style>
</style>
